<?php
// +----------------------------------------------------------------------
// |个人信息
// +----------------------------------------------------------------------
// |Data:2016-06-27 下午12:04
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Wap\Model;
use Common\Model\CommonModel;

class InfoModel extends CommonModel{
    protected $tableName = 'user_info';
    private $user_info_detail_cache_key = 'user_info_detail_';

    public function getUserInfo($uid){
        if(empty($uid)) return false;
        $detail = S($this->user_info_detail_cache_key.$uid);
        if(!$detail){
            $detail = $this->where('uid='.$uid)->find();
            S($this->user_info_detail_cache_key.$uid,$detail);
        }
        return $detail;
    }

    public function clearCache($uid){
        S($this->user_info_detail_cache_key.$uid,null);
    }

    /**
     * 注册
     * @param $openid
     * @param $access_token
     * @return bool|mixed
     */
    public function reg($openid,$access_token){
        if(empty($openid) || empty($access_token)) return false;

        $uid = M('user_login')->where("openid='".$openid."'")->getField('uid');
        if($uid){
            return $uid;
        }

        M()->startTrans();
        $data = array(
            'openid'=>$openid,
        );
        $res = M('user_login')->add($data);

        $arr = array(
            'uid'=>$res,
            'reg_time'=>time(),
        );
        $res_2 = M('user_info')->add($arr);
        if($res && $res_2){
            M()->commit();
            $temp = array(
                'uid'=>$arr['uid'],
                'openid'=>$openid,
                'access_token'=>$access_token
            );
            $data = S('ps_wx_user_base_info_list');
            if($data){
                array_push($data,serialize($temp));
                S('ps_wx_user_base_info_list',$data);
            }else{
                $arr = array(serialize($temp));
                S('ps_wx_user_base_info_list',$arr);
            }
            $this->clearCache($res);
            return $res;
        }else{
            M()->rollback();
            return false;
        }

    }
}
?>